import pyglet
from pyglet.gl import *

import atb
from atb.pyglet_handler import ATBEventHandler

window = pyglet.window.Window()

@window.event
def on_close():
    pyglet.app.exit()


if __name__ == '__main__':
    atb.init()
    atbhandler = ATBEventHandler(window)

    bar = atb.Bar(label="some standard variables",
                  help="some standard variables",
                  color=(200, 50, 50, 50),
                  position=(0, 0),
                  size=(200, 200),
            
    )
    myVar = c_float()
    COLOR4F = c_float * 4
    myColor = COLOR4F()
    myColor2 = COLOR4F()
    bar.add_var("float", myVar)
    bar.add_var("color", myColor, open=True)
    bar.add_var("color RO", myColor2, read_only=True)
    bar.add_separator("my sep")
    boolvar = c_int()
    bar.add_var("bool", boolvar, typ=atb.TW_TYPE_BOOL16)

    buttonbar = atb.Bar('buttons', position=(200, 0))

    def say_hello(data):
        print "hello", data

    def add_stuff(data):
        d = len(buttonbar)
        buttonbar.add_button("button %d" % d, say_hello, d)

    def remove_stuff(data):
        if len(buttonbar) > 2:
            buttonbar.remove("button %d" % (len(buttonbar)-1))

    def clear_all(data):
        print buttonbar.name
        buttonbar.clear()

    buttonbar.add_button("add stuff", add_stuff)
    buttonbar.add_button("remove stuff", remove_stuff)
    buttonbar.add_button("clear", clear_all)

    theval = 1.0

    def getval(data):
        print "get",data
        return theval

    def setval(val, data):
        global theval
        print "set", data
        theval = val
        
    bar = atb.Bar('getsetter', position=(400, 0))
    bar.add_var("gettersetter", typ=c_float, getter=getval, setter=setval, data='a')
    bar.add_var("gettersetter RO", typ=c_float, getter=getval, data='b')

    db = atb.DictBar(values={'a': 1, 'b':2})

    def test(data):
        print db['a']
        print db['b']
    db.add_button("test", test)

    pyglet.app.run()



